{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "editable": true,
    "executionInfo": {
     "elapsed": 18028,
     "status": "ok",
     "timestamp": 1719334607544,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "C8HRr5VTx2AH",
    "outputId": "b71a08bb-60ac-4af2-f8df-34d28da77cd5",
    "slideshow": {
     "slide_type": ""
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Requirement already satisfied: sklearn2pmml==0.40.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (0.40.0)\n",
      "Requirement already satisfied: scikit-learn>=0.18.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from sklearn2pmml==0.40.0) (1.5.1)\n",
      "Requirement already satisfied: sklearn-pandas>=0.0.10 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from sklearn2pmml==0.40.0) (2.2.0)\n",
      "Requirement already satisfied: numpy>=1.19.5 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn>=0.18.0->sklearn2pmml==0.40.0) (1.26.4)\n",
      "Requirement already satisfied: scipy>=1.6.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn>=0.18.0->sklearn2pmml==0.40.0) (1.13.1)\n",
      "Requirement already satisfied: joblib>=1.2.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn>=0.18.0->sklearn2pmml==0.40.0) (1.4.2)\n",
      "Requirement already satisfied: threadpoolctl>=3.1.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn>=0.18.0->sklearn2pmml==0.40.0) (3.5.0)\n",
      "Requirement already satisfied: pandas>=1.1.4 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from sklearn-pandas>=0.0.10->sklearn2pmml==0.40.0) (2.2.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from pandas>=1.1.4->sklearn-pandas>=0.0.10->sklearn2pmml==0.40.0) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from pandas>=1.1.4->sklearn-pandas>=0.0.10->sklearn2pmml==0.40.0) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from pandas>=1.1.4->sklearn-pandas>=0.0.10->sklearn2pmml==0.40.0) (2024.1)\n",
      "Requirement already satisfied: six>=1.5 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas>=1.1.4->sklearn-pandas>=0.0.10->sklearn2pmml==0.40.0) (1.16.0)\n",
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Requirement already satisfied: scikit-learn==1.5.1 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (1.5.1)\n",
      "Requirement already satisfied: numpy>=1.19.5 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn==1.5.1) (1.26.4)\n",
      "Requirement already satisfied: scipy>=1.6.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn==1.5.1) (1.13.1)\n",
      "Requirement already satisfied: joblib>=1.2.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn==1.5.1) (1.4.2)\n",
      "Requirement already satisfied: threadpoolctl>=3.1.0 in /Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages (from scikit-learn==1.5.1) (3.5.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install sklearn2pmml==0.40.0\n",
    "!pip install scikit-learn==1.5.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "editable": true,
    "executionInfo": {
     "elapsed": 2185,
     "status": "ok",
     "timestamp": 1719334610779,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "o3EpO2HixtGI",
    "slideshow": {
     "slide_type": ""
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "\n",
    "\n",
    "from sklearn.datasets import load_iris\n",
    "\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "from sklearn.pipeline import Pipeline\n",
    "\n",
    "\n",
    "from sklearn2pmml.decoration import ContinuousDomain\n",
    "from sklearn2pmml.pipeline import PMMLPipeline\n",
    "\n",
    "from sklearn2pmml import sklearn2pmml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "executionInfo": {
     "elapsed": 6,
     "status": "ok",
     "timestamp": 1719334610780,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "hS3FYrGoxtGi",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "iris_data = load_iris()\n",
    "\n",
    "X = pd.DataFrame(data=iris_data.data,\n",
    "                 columns=['_'.join(feature_name.split()[:2]) for feature_name in iris_data.feature_names])\n",
    "y = pd.DataFrame(data=iris_data.target,\n",
    "                 columns=['species'])\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,\n",
    "                                                    y,\n",
    "                                                    test_size=.1,\n",
    "                                                    stratify=y,\n",
    "                                                    random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "executionInfo": {
     "elapsed": 334,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "Hrpo_mTVxtGk",
    "outputId": "91c8b856-33be-45b2-dffb-2e4029ce56af",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal_length</th>\n",
       "      <th>sepal_width</th>\n",
       "      <th>petal_length</th>\n",
       "      <th>petal_width</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>4.4</td>\n",
       "      <td>2.9</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal_length  sepal_width  petal_length  petal_width  species\n",
       "29            4.7          3.2           1.6          0.2        0\n",
       "145           6.7          3.0           5.2          2.3        2\n",
       "8             4.4          2.9           1.4          0.2        0\n",
       "148           6.2          3.4           5.4          2.3        2\n",
       "25            5.0          3.0           1.6          0.2        0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris_train = pd.concat([X_train,\n",
    "                        y_train],\n",
    "                       axis=1)\n",
    "iris_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "executionInfo": {
     "elapsed": 5,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "3vD-bUEdxtGm",
    "outputId": "3db0af17-3700-4f18-972d-9a00d231dd66",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal_length</th>\n",
       "      <th>sepal_width</th>\n",
       "      <th>petal_length</th>\n",
       "      <th>petal_width</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>6.6</td>\n",
       "      <td>2.9</td>\n",
       "      <td>4.6</td>\n",
       "      <td>1.3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>6.1</td>\n",
       "      <td>2.6</td>\n",
       "      <td>5.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>5.6</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.9</td>\n",
       "      <td>1.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>7.3</td>\n",
       "      <td>2.9</td>\n",
       "      <td>6.3</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal_length  sepal_width  petal_length  petal_width  species\n",
       "58            6.6          2.9           4.6          1.3        1\n",
       "134           6.1          2.6           5.6          1.4        2\n",
       "147           6.5          3.0           5.2          2.0        2\n",
       "69            5.6          2.5           3.9          1.1        1\n",
       "107           7.3          2.9           6.3          1.8        2"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris_test = pd.concat([X_test,\n",
    "                       y_test],\n",
    "                      axis=1)\n",
    "iris_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "lK6FzQeZxtGm",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/eric/miniconda3/envs/py310/lib/python3.10/site-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function tosequence is deprecated; tosequence was deprecated in 1.5 and will be removed in 1.7\n",
      "  warnings.warn(msg, category=FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "pipeline = PMMLPipeline([\n",
    "    ('mapper',\n",
    "     DataFrameMapper([\n",
    "         (X_train.columns.values,\n",
    "          [ContinuousDomain(),\n",
    "           SimpleImputer(),\n",
    "           StandardScaler()])])),\n",
    "    ('pca',\n",
    "     PCA(n_components=4)),\n",
    "    ('selector',\n",
    "     SelectKBest(k=2)),\n",
    "    ('classifier',\n",
    "     DecisionTreeClassifier())\n",
    "])\n",
    "\n",
    "pipeline.fit(iris_train.drop('species',axis=1),\n",
    "             iris_train['species']);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "iEV6NtK3_hOZ",
    "outputId": "82670b43-8f86-42b4-867b-042b1c6b2821",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        45\n",
      "           1       1.00      1.00      1.00        45\n",
      "           2       1.00      1.00      1.00        45\n",
      "\n",
      "    accuracy                           1.00       135\n",
      "   macro avg       1.00      1.00      1.00       135\n",
      "weighted avg       1.00      1.00      1.00       135\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(pipeline.predict(X_train),\n",
    "                            y_train))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 3,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "hNgPE7NJxtGn",
    "outputId": "f6b07a51-ec62-498d-9ce1-0c776beffcad",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, 1, 2, 0, 0, 0, 2, 1, 0, 2, 1, 2, 0])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 3,
     "status": "ok",
     "timestamp": 1719334611108,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "HWmQ4HjT2TjH",
    "outputId": "4924b0dc-58aa-49b2-bf19-8c6cc687bec5",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 2, 1, 2, 0, 0, 0, 2, 1, 0, 2, 1, 1, 0])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.values.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "editable": true,
    "executionInfo": {
     "elapsed": 177,
     "status": "ok",
     "timestamp": 1719334611283,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "HXJ0NCPu_MWs",
    "outputId": "4f124a84-f8ad-4899-a0e4-6f5f786c1f6b",
    "slideshow": {
     "slide_type": ""
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00         5\n",
      "           1       0.60      1.00      0.75         3\n",
      "           2       1.00      0.71      0.83         7\n",
      "\n",
      "    accuracy                           0.87        15\n",
      "   macro avg       0.87      0.90      0.86        15\n",
      "weighted avg       0.92      0.87      0.87        15\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(pipeline.predict(X_test),\n",
    "                            y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": true,
    "executionInfo": {
     "elapsed": 8658,
     "status": "ok",
     "timestamp": 1719334619939,
     "user": {
      "displayName": "Jawahar Panchal",
      "userId": "01949585979586398994"
     },
     "user_tz": 300
    },
    "id": "LUSdRDyOxtGo",
    "slideshow": {
     "slide_type": ""
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from sklearn2pmml import sklearn2pmml\n",
    "sklearn2pmml(pipeline, 'iris_pipeline.pmml', with_repr=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}